package cn.itcast.bos.web.action.take_delivery;

import cn.itcast.bos.domain.take_delivery.Promotion;
import cn.itcast.bos.service.base.PromotionService;
import cn.itcast.bos.util.base.BaseAction;
import com.opensymphony.xwork2.ActionContext;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;


import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Controller
@ParentPackage("json-default")
@Namespace("/")
@Scope("prototype")
public class PromotionAction extends BaseAction<Promotion> {

    private File titleImgFile;
    private  String titleImgFileFileName;

    public void setTitleImgFile(File titleImgFile) {
        this.titleImgFile = titleImgFile;
    }

    public void setTitleImgFileFileName(String titleImgFileFileName) {
        this.titleImgFileFileName = titleImgFileFileName;
    }

    @Autowired
    private PromotionService promotionService;

    @Action(value = "promotion_save",results = {
            @Result(name = "success",type = "redirect",location = "./pages/take_delivery/promotion.html")
    })
    public  String save() throws IOException {
        //上传宣传图片
        String savePath = ServletActionContext.getServletContext().getRealPath("/upload/");
        String saveUrl = ServletActionContext.getRequest().getContextPath()+"/upload/";

        //生成随机图片名
        String uuid =UUID.randomUUID().toString();
        String ext =titleImgFileFileName.substring(titleImgFileFileName.lastIndexOf("."));
        String randomFileName = uuid +ext;
        //保存图片
        File  destFile = new File(savePath +"/" + randomFileName);

        System.out.println(saveUrl+ randomFileName);
        FileUtils.copyFile(titleImgFile,destFile);
    //   model.setTitleImg(saveUrl+ randomFileName);
      model.setTitleImg(ServletActionContext.getRequest().getContextPath()+ "/upload/" + randomFileName);
      //  model.setTitleImg( "/bos_management/upload/" + randomFileName);
        System.out.println(ServletActionContext.getRequest().getContextPath());
        System.out.println("1111111111111111111s");
            promotionService.save(model);
        return  SUCCESS;

    }
    @Action(value = "promotion_pageQuery",results = {
            @Result(name = "success",type = "json")
    })

    public  String pageQuery (){
        Pageable pageable = new PageRequest(page-1,rows);
        Page<Promotion> pageData = promotionService.findPageData(pageable);
        Map<String,Object> map = new HashMap<>();
        map.put("total",pageData.getNumberOfElements());
        map.put("rows",pageData.getContent());
        ActionContext.getContext().getValueStack().push(map);
        return  SUCCESS;
}
}
